package com.example.admin.SQLite.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.User;

import java.util.ArrayList;

import static com.example.admin.SQLite.SqLiteOpenHelper.TABLE_USER;

public class UserDao {
    SqLiteOpenHelper dbHelper;
    private static final String KEY_ID = "id";

    public UserDao(Context context) {
        dbHelper = new SqLiteOpenHelper(context);

    }

    public ArrayList<User> findWhereUser(String userName) {

        String selection = "userName=?"; // userName为要查询的列名
        String[] selectionArgs = {userName}; // value为要查询的值
        SQLiteDatabase db = dbHelper.getSqlite();
        Cursor cursor = db.query(TABLE_USER, null, selection, selectionArgs, null, null, null);

        ArrayList<User> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            User model = new User();
            model.id = cursor.getInt(0);
            model.userName = cursor.getString(1);
            model.UserPass = cursor.getString(2);
            model.UserAddress = cursor.getString(3);
            arrContacts.add(model);

        }
        return arrContacts;
    }
    public void addUserData(User bean){
        SQLiteDatabase db = dbHelper.getSqlite();
        Log.e("发的地方", "addUserData: "+bean.toString() );
        ContentValues values = new ContentValues();
        values.put("userName",bean.userName);
        values.put("UserPass",bean.UserPass);
        values.put("UserAddress",bean.UserAddress);
        db.insert(TABLE_USER, null,values);
    }

    public void updateUserPass(int id,String pass){

        SQLiteDatabase database =dbHelper.getSqlite();

        ContentValues cv = new ContentValues();
        cv.put("UserPass",pass);

        database.update(TABLE_USER,cv,KEY_ID+" = "+ id,null);
    }

    public ArrayList<User> findWhereUsers(String id) {

        String selection = "id=?"; // userName为要查询的列名
        String[] selectionArgs = {id}; // value为要查询的值
        SQLiteDatabase db = dbHelper.getSqlite();
        Cursor cursor = db.query(TABLE_USER, null, selection, selectionArgs, null, null, null);

        ArrayList<User> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            User model = new User();
            model.id = cursor.getInt(0);
            model.userName = cursor.getString(1);
            model.UserPass = cursor.getString(2);
            model.UserAddress = cursor.getString(3);
            arrContacts.add(model);

        }
        return arrContacts;
    }
    public ArrayList<User> getUserList() {
        SQLiteDatabase db = dbHelper.getSqlite();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_USER, null);

        ArrayList<User> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            User model = new User();
            model.id = cursor.getInt(0);
            model.userName = cursor.getString(1);
            model.UserPass = cursor.getString(2);
            model.UserAddress = cursor.getString(3);

            arrContacts.add(model);

        }
        return arrContacts;
    }
}
